'''
Created on Mar 14, 2016

@author: JimmyMo
'''
import time
import inspect
from common import commonutil

class Processor(object):
    def __init__(self):
        pass
    def set_successor(self, successor):
        self.successor = successor
        return successor
        
    def process(self, current_event_list, prior_event_dict):
        self.current_event_list = current_event_list
        self.prior_event_dict = prior_event_dict
        
        real_class_name =  self.__class__.__name__
        now = time.clock()
        time.clock()
        
        self._process(current_event_list, prior_event_dict)
        elapsed = time.clock() - now
        print("%s processing took %fs" % (real_class_name, elapsed))
        if self.successor:
            self.successor.process(current_event_list, prior_event_dict)
       
        
    def _process(self, current_event_list, prior_event_dict):
        for e in current_event_list:
#             if 'portname' in e:
#                 portname = e['portname']
#             else:
#                 portname = '_default'
#             portname = commonutil.get_port_name(e, 'portname', commonutil.DEFAULT_PORTNAME)
#             key = "%s_%s" % (e['devicename'], portname)
            key = commonutil.get_key(e)
            prior_e = prior_event_dict[key]
            self._process_event(e, prior_e)
#         '''abstract method'''
#         class_name = self.__class__.__name__
#         method_name = inspect.stack()[0][3]
#         raise Exception("%s method in class %s is abstract method which is not implemented..." % (method_name, class_name))
    def _process_event(self, current_event, prior_event):
        '''abstract method'''
        class_name = self.__class__.__name__
        method_name = inspect.stack()[0][3]
        raise Exception("%s method in class %s is abstract method which is not implemented..." % (method_name, class_name))
    
    def _drop_event(self, e):
        self.current_event_list.remove(e)
#         e["_oper"] = commonutil.OPER_DELETED
    def _new_event(self, e):
        self.current_event_list.append(e)
#         e["_oper"] = commonutil.OPER_ADDED
